/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog;

import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.AddBlogEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetBlogEditDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetBlogByIdEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetSetBlogPreviewDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetListUserBlogTitleEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.RemoveBlogEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.UpdateBlogEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerRegistry;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.AddEventRegistryListenerEvent;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.RemoveEventRegistryListenerEvent;

/**
 * 
 * @author jens.meiss
 */
public class BlogRemoteServiceController extends AControllerRegistry {

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == GetBlogByIdEvent.class) {		
			
			BlogAsynCallback.getBlog(((GetBlogByIdEvent) event).getId(), ((GetBlogByIdEvent) event).getEventListener());	
			
		} else if (event.getClass() == AddBlogEvent.class) {
			
			BlogAsynCallback.addBlog(((AddBlogEvent) event).getBlogNewDTO(), ((AddBlogEvent) event).getEventListener());
			
		} else if (event.getClass() == UpdateBlogEvent.class) {
			
			BlogAsynCallback.update(((UpdateBlogEvent) event).getBlogEditDTO() , ((UpdateBlogEvent) event).getEventListener());
			
		} else if (event.getClass() == RemoveBlogEvent.class) {
			
			BlogAsynCallback.removeBlog(((RemoveBlogEvent) event).getBlogRemoveDTO() , ((RemoveBlogEvent) event).getEventListener());
			
		} else if (event.getClass() == GetSetBlogPreviewDTOEvent.class) {
			
			BlogAsynCallback.getSetBlogPreview(((GetSetBlogPreviewDTOEvent) event).getEventListener());
			
		} else if (event.getClass() == GetListUserBlogTitleEvent.class) {
			
			BlogAsynCallback.getListBlogTitleDTOEvent(((GetListUserBlogTitleEvent) event).getEventListener());
			
		} else if (event.getClass() == GetBlogEditDTOEvent.class){
			
			BlogAsynCallback.getEditBlogDTO((((GetBlogEditDTOEvent) event).getBlogEditIdDTO()), (((GetBlogEditDTOEvent) event).getEventListener()));
			
		}
	}

	public void init() {
		
		fireEventGlobal(new AddEventRegistryListenerEvent(GetBlogByIdEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(AddBlogEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(UpdateBlogEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(RemoveBlogEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(GetSetBlogPreviewDTOEvent.class, this));	
		fireEventGlobal(new AddEventRegistryListenerEvent(GetListUserBlogTitleEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(GetBlogEditDTOEvent.class, this));
		
	}

	public void terminate() {
		
		fireEventGlobal(new RemoveEventRegistryListenerEvent(GetBlogByIdEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(AddBlogEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(UpdateBlogEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(RemoveBlogEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(GetSetBlogPreviewDTOEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(GetListUserBlogTitleEvent.class, this));

	}

}
